package com.example.file;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class rename extends Activity{
	private File path;
	private File[] filelist;
	private int[] select;
	private ListView listview;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rename);
		listview=(ListView) findViewById(R.id.listView1);
		path=(File)getIntent().getExtras().get("path");
		select=(int[]) getIntent().getExtras().get("select");
		filelist=path.listFiles();
		filelist=MainActivity.groupbyname(filelist);
		listview.setAdapter(adapter);
		Button back=(Button) findViewById(R.id.back);
		Button save=(Button) findViewById(R.id.save);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				save();
			}
		});
	}
	
	BaseAdapter adapter=new BaseAdapter() {
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			EditText t=new EditText(rename.this);
			t.setId(position);
			t.setText(""+filelist[select[position]].getName());
			t.setPadding(0, 30, 0, 10);
			return t;
		}
		
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
		
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}
		
		@Override
		public int getCount() {
			return select.length;
		}
	};
	public void save(){
		for(int i=0;i<select.length;i++){
			EditText edt=(EditText) listview.getChildAt(i);
			filelist[select[i]].renameTo(new File(path+"/"+edt.getText().toString()));
		}
		onBackPressed();
	}
}
